package com.zmn.plat.business.impl.product;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.product.ServSubsidiaryItemBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.product.ServSubsidiaryItemDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.product.ServProduct;
import com.zmn.plat.model.entity.product.ServSubsidiaryGroupItem;
import com.zmn.plat.model.entity.product.ServSubsidiaryGroupItemQuery;
import com.zmn.plat.model.entity.product.ServSubsidiaryItem;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.plat.services.interfaces.product.ServSubsidiaryGroupItemService;
import com.zmn.plat.services.interfaces.product.ServSubsidiaryItemService;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述:ServSubsidiaryItemBServiceImpl
 *
 * @author tanghongjun
 * @since: 2018/10/16 17:56
 */
@Service
@Slf4j
public class ServSubsidiaryItemBServiceImpl implements ServSubsidiaryItemBService {

    @Resource
    protected ServSubsidiaryItemService servSubsidiaryItemService;
    @Resource
    protected ServProductService servProductService;
    @Resource
    protected ServSubsidiaryGroupItemService servSubsidiaryGroupItemService;

    @Override
    public List<ServSubsidiaryItemDRO> listByProductIdAndType(Integer productId, Integer type) {
        if (NumberUtil.isNullOrZero(productId) || NumberUtil.isNullOrZero(type)) {
            log.error("listByProductIdAndType 参数不合法.productId:{},type:{}", productId, type);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ServProduct servProduct = servProductService.findByKey(productId);
        if (servProduct == null) {
            log.error("产品不存在.productId:{},errorCode:{}", productId, PlatErrorCodeDict.ERROR_CODE_PRODUCT_SUBSIDIARY_ITEM_NO_PRODUCT);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PRODUCT_SUBSIDIARY_ITEM_NO_PRODUCT);
        }

        Integer groupId = servProduct.getWarrantyGroupId();
        if (ProductDict.SUBSIDIARY_TYPE_FAULT.equals(type)) {
            groupId = servProduct.getFaultGroupId();
        }

        ServSubsidiaryGroupItemQuery servSubsidiaryGroupItemQuery = new ServSubsidiaryGroupItemQuery();
        servSubsidiaryGroupItemQuery.setGroupId(groupId);
        List<ServSubsidiaryGroupItem> groupItemList = servSubsidiaryGroupItemService.listByQuery(servSubsidiaryGroupItemQuery);

        if (CollectionUtils.isEmpty(groupItemList)) {
            log.error("该产品没有对应分组.groupId:{},errorCode:{}", groupId, PlatErrorCodeDict.ERROR_CODE_PRODUCT_SUBSIDIARY_ITEM_NO_GROUP);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PRODUCT_SUBSIDIARY_ITEM_NO_GROUP);
        }

        List<Integer> itemIdList = groupItemList.stream().map(ServSubsidiaryGroupItem::getItemId).collect(Collectors.toList());
        List<ServSubsidiaryItem> servSubsidiaryItems = servSubsidiaryItemService.findByKeys(itemIdList);
        BeanCopier copier = BeanCopier.create(ServSubsidiaryItem.class, ServSubsidiaryItemDRO.class, false);
        List<ServSubsidiaryItemDRO> dros = servSubsidiaryItems.stream().map(servSubsidiaryItem -> {
            ServSubsidiaryItemDRO servSubsidiaryItemDRO = new ServSubsidiaryItemDRO();
            copier.copy(servSubsidiaryItem, servSubsidiaryItemDRO, null);
            return servSubsidiaryItemDRO;
        }).collect(Collectors.toList());

        return dros;
    }

    @Override
    public Integer updateStatus(Integer itemId, Integer status, String updater) {
        ServSubsidiaryItem servSubsidiaryItem = new ServSubsidiaryItem();
        servSubsidiaryItem.setItemId(itemId);
        servSubsidiaryItem.setStatus(status);
        servSubsidiaryItem.setUpdater(updater);
        return this.updateStatus(servSubsidiaryItem);
    }

    @Override
    public Integer updateStatus(ServSubsidiaryItem servSubsidiaryItem) {

        int pos = servSubsidiaryItemService.updateStatus(servSubsidiaryItem);

        return pos;
    }

    @SuppressWarnings("Duplicates")
    @Override
    public Integer updateStatusBatch(List<Integer> itemIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(itemIds);
        dto.setStatus(status);
        dto.setUpdater(updater);

        return this.updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {

        int pos = servSubsidiaryItemService.updateStatusBatch(dto);

        return pos;
    }
}